// pages/uploading/uploading.js
import { Uploading } from '../../model/uploading.js'
const uploading = new Uploading();
Page({

    /**
     * 页面的初始数据
     */
    data: {
      piclist:[],//上传图片的数组
      inptit:'',
      textarea:'',
      id:'',
      picWidth:'',
      picHeight:'',
      isJoin:0,
      notice:'',
      upcode:0
    },
    // 返回
    handlerGobackClick() {
        wx.navigateBack({
            delta: 1
        })
    },
    //上传作品
    addwork(e) {
      // wx.showToast({
      //   title: 'ceshi',
      //   icon:'none',
      //   duration:1000,
      // })
      // // var pages = getCurrentPages(); //当前页面
      // //     var beforePage = pages[pages.length - 2]; //前一页
      // //     wx.navigateBack({
      // //        delta:1,
      // //         success: function () {
      // //             // beforePage.onLoad(); // 执行前一个页面的onLoad方法
      // //         }
      // //     });
      
      // return false;




      var that=this
      if(that.data.inptit=='' || that.data.inptit==null){
        wx.showToast({
          title: '请填写标题',
          icon:"none"
        })
        return false;
      }else if(that.data.inptit==''){
          wx.showToast({
            title: '标题不能为空',
            icon:'none'
          })
        return false;
      }else if(that.data.textarea ==''){
          wx.showToast({
            title: '作品描述不能为空',
            icon:"none"
          })
          return false;
      }else if(that.data.piclist.length==0){
        wx.showToast({
          title: '您上传的图片不能为空',
          icon:'none'
        })
        return false;
    }else{
      
      
      
      if(that.data.isJoin==0){
        
        wx.showModal({
          cancelColor: '#000000',
          title:'提示',
          content:'您确认要发布吗？',
          showCancel:true,
          success (res) {
            if (res.confirm) {
            console.log('用户点击确定');
            
            that.work_uploading()
                // var notice=res.data.notice
                  
            } else if (res.cancel) {
            console.log('用户点击取消')
            }
          }
        })
      }else if(that.data.isJoin==1){
        


        var that = this;

        wx.showModal({
          cancelColor: '#000000',
          title:'提示',
          content:'您已发布过作品，再次上传将覆盖原作品，您确认要发布吗？',
          showCancel:true,
          success (res) {
            if (res.confirm) {
            console.log('用户点击确定')
            that.work_uploading()
              // wx.showToast({
              //   title: '发布成功',
              //   icon:'none',
              //   duration:2000
              // })
              // setTimeout(()=>{
              //   var pages = getCurrentPages(); //当前页面
              //     var beforePage = pages[pages.length - 2]; //前一页
              //     wx.navigateBack({
              //        delta:1,
              //         success: function () {
              //             // beforePage.onLoad(); // 执行前一个页面的onLoad方法
              //         }
              //     });
              // },2000)
            } else if (res.cancel) {
            console.log('用户点击取消')
            }
          }
        })
      }
    }
        // if(that.data.piclist.length==0){
        //     wx.showToast({
        //       title: '您上传的图片不能为空',
        //       icon:'none'
        //     })
        // }else{
        //     this.work_uploading()
        // }
        
    },
    //绑定inp
    work_tit(e){
        // console.log(e)
        let that = this;
        if (e.detail.value.length < 1) {
            that.setData({
                inptit: '标题描述',
            })
        } else {
            that.setData({
                inptit: e.detail.value,
            })
        }
    },
    //
    work_text(e){
        console.log(e)
        let that = this;
        if (e.detail.value.length < 1) {
            that.setData({
                textarea: '内容描述',
            })
        } else {
            that.setData({
                textarea: e.detail.value,
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        this.setData({
            id:options.id,
            isJoin:options.isJoin
        })
    },
    
    // 上传
    upimages(){
        var that = this
        wx.chooseImage({

          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success(res) {
            console.log(res)
            const tempFilePaths = res.tempFilePaths
            that.uploadsome(tempFilePaths)
          },
          filess(err) {
            console.log(err)
          },

        })
    },
    // 上传图片或者视频

    uploadsome: function (tempFilePaths) {
      var that = this
      
      console.log(tempFilePaths)
      wx.showLoading({
        title: '上传中',
      })

      wx.uploadFile({
        // url: 'http://192.168.0.100:9377/qiniu/upload_file',
        url:'https://api.changetraining.com.cn/qiniu/upload_file',
        filePath: tempFilePaths[0],
        name: 'file',
        formData: {
          'user': 'test',
        },
        success(res) {
          console.log(res.data);
          console.log(res)
          var msg = JSON.parse(res.data)
          var list = that.data.piclist
          var list = []
          
          list.push(msg.data)
          that.setData({
              piclist: list
              
            })
            //   console.log(that.data.piclist.picHeight)
          for(var i=0;i<that.data.piclist.length;i++){
              console.log(that.data.piclist[i])
              that.setData({
                picHeight:that.data.piclist[i].picHeight,
                picWidth:that.data.piclist[i].picWidth,
                picUrl:that.data.piclist[i].fileName,

              })
          }
          wx.hideLoading()
        },
        fail(error) {
          console.log(error);
        }
        
      })


    },
    //上传作品
    work_uploading(){
      var that=this
      var obj={
        worksId:that.data.id,
        title:that.data.inptit,
        content:that.data.textarea,
        picUrl:that.data.piclist[0].fileName,
        picWidth:that.data.piclist[0].picWidth,
        picHeight:that.data.piclist[0].picHeight,
    }
    uploading.save_work(obj).then(res=>{
        console.log(res)
        if(res.code ==0){
          this.setData({
            notice:res.data.notice,
            upcode:res.code,
          })
          var title_content = "发布成功";
          if(res.data.notice !=''){
            title_content = res.data.notice;
          }
          wx.showToast({
            title: title_content,
            icon:'none',
            duration:2000,
          })
          setTimeout(()=>{
            var pages = getCurrentPages(); //当前页面
              var beforePage = pages[pages.length - 2]; //前一页
              wx.navigateBack({
                 delta:1,
                  success: function () {
                      // beforePage.onLoad(); // 执行前一个页面的onLoad方法
                  }
              });
          },500)
        }else if(res.code ==1){
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration:2000,
          })
        }
       


    }).catch(error=>{
        console.log(error)
    })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})